/*
 * Copyright (C) 2009-2018 Hangzhou 2Dfire Technology Co., Ltd. All rights reserved
 */
package com.ws.soa.consumer.service;

import com.alibaba.fastjson.JSONObject;
import com.ws.soa.consumer.constant.Constants;
import com.ws.soa.consumer.util.SmsUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * MessageService
 * 短信发送服务，这里要将验证码保存到 redis 当中
 * 短信验证码只能保证五分钟之内有效
 *
 * @author luobosi@2dfire.com
 * @since 2019-04-01
 */
@Service
public class MessageService {

    @Resource
    private RedisCacheClientService redisCacheClientService;

    private static final String TEMPLATE_CODE = "SMS_158035677";



    /**
     * 根据手机号发送短信验证码
     *
     * @param mobile 手机号
     */
    public void sendVerifyCode(String mobile) {
        // 短信发送
        Map<String, String> templateParamMap = new HashMap<>(2, 1);
        // templateParamMap.put("name", familyInfoDTO.getRealName());
        // templateParamMap.put("plan", Objects.requireNonNull(DeepPlanEnum.getByPlan(familyInfoDTO.getPlanType())).getDesc());
        SmsUtils.sendSimpleMessage(mobile, JSONObject.toJSONString(templateParamMap), TEMPLATE_CODE);

        String verifyCode = "短信验证码";
        // 将验证码保存到redis 里面，保存五分钟
        redisCacheClientService.set(Constants.Default.VERIFY_CODE + mobile, verifyCode, Constants.CacheTime.FIVE_MINUTE);

    }

}
